package com.watson.webService.config;

/**
 * // todo
 *
 * @author: llun
 * @date: 2022/2/16 15:41
 * @version: 1.0
 */

import com.watson.webService.service.DemoService;
import com.watson.webService.service.impl.DemoServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean createDispatcherServlet() {
        // 设置webservice访问父路径
        return new ServletRegistrationBean(new CXFServlet(), "/demo/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public DemoService demoService() {
        return new DemoServiceImpl();
    }


// 此处可以采用构造方法初始化或者bean注入

//    private final Bus bus;
//    private final DemoService messageService;
//    private final WebServiceAuthInterceptor webServiceAuthInterceptor;
//
//    @Autowired
//    public CXFConfig(Bus bus, DemoService demoService, WebServiceAuthInterceptor webServiceAuthInterceptor) {
//        this.bus = bus;
//        this.demoService = demoService;
//        this.webServiceAuthInterceptor = webServiceAuthInterceptor;
//    }

    @Autowired
    WebServiceAuthInterceptor webServiceAuthInterceptor;

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
        endpoint.publish("/api");
//       如果需要设置用户密码、 添加拦截器
        endpoint.getInInterceptors().add(webServiceAuthInterceptor);
        return endpoint;
    }

}